package com.woniu.liuzhougov.itemAndTask.service.impl;

import com.woniu.liuzhougov.authorityControl.pojo.User;
import com.woniu.liuzhougov.itemAndTask.dao.ItemTaskMapper;
import com.woniu.liuzhougov.itemAndTask.dao.SupervisionMapper;
import com.woniu.liuzhougov.itemAndTask.pojo.Item;
import com.woniu.liuzhougov.itemAndTask.pojo.ItemTask;
import com.woniu.liuzhougov.itemAndTask.pojo.Supervision;
import com.woniu.liuzhougov.itemAndTask.service.ItemTaskService;
import com.woniu.liuzhougov.itemAndTask.utils.TaskUtil;
import com.woniu.liuzhougov.systemSettings.dao.LogMapper;
import com.woniu.liuzhougov.systemSettings.pojo.Log;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.sql.SQLException;
import java.util.List;

/**
 * @author wyz02
 * @version 1.0
 * @ClassName ItemTaskServiceImpl
 * @Description: 子任务service实现类
 * @date 2019/4/19 19:19
 */

@Service
public class ItemTaskServiceImpl implements ItemTaskService {
    @Autowired
    private ItemTaskMapper itemTaskMapper;
    @Autowired
    private SupervisionMapper supervisionMapper;
    @Autowired
    private LogMapper logMapper;
    /*
    **
     * @Author Wuyuzhong
     * @Description 新增子任务
     * @Date 2019/4/19 19:20
     * @Param [itemTask]
     * @return java.lang.String
     **/
    @Transactional(rollbackFor = Exception.class)
    @Override
    public String addTask(String taskInfo,Integer itemTaskWorker, Integer itemId,String itemUrengcy,User founder) {
        ItemTask itemTask = new ItemTask();
        Item item = new Item();
        item.setItemId(itemId);
        User user = new User();
        user.setUserId(itemTaskWorker);
        itemTask.setItem(item);
        itemTask.setItemTaskWorker(user);
        itemTask.setItemTaskContent(taskInfo);

        //System.out.println(itemUrengcy);

        try {
            int state =itemTaskMapper.addTask(itemTask);
            Log addTaskLog=TaskUtil.getLog(itemId,"分配第"+itemTask.getItemTaskId()+"号任务",founder.getUserId());
            logMapper.insertSelective(addTaskLog);

            if(itemUrengcy.equals("2")){ //紧急

                String context ="督办第"+item.getItemId()+"号事项第"+itemTask.getItemTaskId()+"号子任务";
                Supervision supervision = TaskUtil.getSupervision(founder,user,item,context,itemTask);
                int flag=supervisionMapper.insertSupervision(supervision);
                //System.out.println("生成督办单");
                //System.out.println(user);
                Log addSupervisionLog=TaskUtil.getLog(itemId,"下发第"+itemTask.getItemTaskId()+"号任务督办单",founder.getUserId());
                logMapper.insertSelective(addSupervisionLog);


            }
            return "success";

        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//关键
            return "failed";
        }

        //return "failed";


    }

    /*
    **
     * @Author Wuyuzhong
     * @Description 按事项id查询所有子任务
     * @Date 2019/4/19 19:25
     * @Param [itemId, currentPage, pageSize]
     * @return java.util.List<com.woniu.liuzhougov.itemAndTask.pojo.ItemTask>
     **/
    @Override
    public List<ItemTask> searchTaskByItemId(Integer itemId) {
        return itemTaskMapper.seachTaskByItemId( itemId);
    }
    /*
    **
     * @Author Wuyuzhong
     * @Description 根据子任务id查询子任务
     * @Date 2019/4/19 19:27
     * @Param [taskId]
     * @return com.woniu.liuzhougov.itemAndTask.pojo.ItemTask
     **/
    @Override
    public ItemTask searchTaskById(Integer taskId) {
        return itemTaskMapper.searchTaskById(taskId);
    }

    /*
    **
     * @Author Wuyuzhong
     * @Description 更新子任务
     * @Date 2019/4/19 19:28
     * @Param [itemTask]
     * @return java.lang.String
     **/
    @Transactional(rollbackFor = Exception.class)
    @Override
    public String updateTask(String taskInfo,Integer itemTaskWorker,Integer itemId,String itemUrengcy,Integer itemTaskId) {

        ItemTask itemTask = new ItemTask();
        Item item = new Item();
        item.setItemId(itemId);
        User user = new User();
        user.setUserId(itemTaskWorker);
        itemTask.setItem(item);
        itemTask.setItemTaskWorker(user);
        itemTask.setItemTaskContent(taskInfo);
        itemTask.setItemTaskId(itemTaskId);

       // System.out.println(itemTask);

        try {
            int state=itemTaskMapper.updateTask(itemTask);
            User founder = (User) SecurityUtils.getSubject().getPrincipal();
            Log addTaskLog=TaskUtil.getLog(itemId,"分配第"+itemTask.getItemTaskId()+"号任务",founder.getUserId());

            logMapper.insertSelective(addTaskLog);
            if(itemUrengcy.equals("2")){ //紧急
                String context ="督办第"+item.getItemId()+"号事项第"+itemTask.getItemTaskId()+"号子任务(修改)";
                //System.out.println("修改督办单");
                Supervision supervision= TaskUtil.getSupervision(null,user,item,context,itemTask);
                int flag = supervisionMapper.updateSupervision(supervision);

                Log addSupervisionLog=TaskUtil.getLog(itemId,"更新第"+itemTask.getItemTaskId()+"号任务督办单",founder.getUserId());
                logMapper.insertSelective(addSupervisionLog);


            }
            return "success";
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//关键
            return "failed";
        }


    }

    /*
    **
     * @Author Wuyuzhong
     * @Description 通过id删除子任务
     * @Date 2019/4/19 19:30
     * @Param [itemTaskId]
     * @return java.lang.String
     **/
    @Transactional(rollbackFor = Exception.class)
    @Override
    public String deleteByPrimaryKey(Integer itemTaskId,Integer urengcyId) {

        try {
            if(urengcyId==2){ //紧急

                //System.out.println("撤销督办单");
                int result= supervisionMapper.deleteByTaskId(itemTaskId);
                //短信接口
                int state= itemTaskMapper.deleteByPrimaryKey(itemTaskId);
                return "success";
            }else {
                int state= itemTaskMapper.deleteByPrimaryKey(itemTaskId);
                return "success";
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//关键
            return "failed";
        }


    }

    /*
    **
     * @Author Wuyuzhong
     * @Description 通过id更新任务状态
     * @Date 2019/4/19 19:34
     * @Param [itemTaskId, taskStateId]
     * @return java.lang.String
     **/
    @Transactional(rollbackFor = Exception.class)
    @Override
    public String updateTaskState(Integer itemTaskId, Integer taskStateId,Integer urengcyId) {

        try {
            if(urengcyId==2){ //紧急


                //System.out.println("撤销督办单");
                int result= supervisionMapper.deleteByTaskId(itemTaskId);
                //短信接口
                int state = itemTaskMapper.updateTaskState(itemTaskId,taskStateId);
                return "success";
            }else {
                int state = itemTaskMapper.updateTaskState(itemTaskId,taskStateId);
                return "success";
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//关键
            return "failed";
        }


    }

    /*
    **
     * @Author Wuyuzhong
     * @Description 通过执行人id获取子任务列表
     * @Date 2019/4/28 16:56
     * @Param [workerId]
     * @return java.util.List<com.woniu.liuzhougov.itemAndTask.pojo.ItemTask>
     **/
    @Override
    public List<ItemTask> seachTaskByWorkerId(Integer workerId) {
        
        return itemTaskMapper.seachTaskByWorkerId(workerId);
    }



}
